
var timerButton = document.getElementById('timer')
var messageButton = document.getElementById('message')


var counter = 1


/*
  
  class Promise<T>(cb)



  class Promise<T>(cb: (complete, fail) => void)
  
                     complete: (value: T) => void
                     fail: (error: Error) => void


  class Promise<T>(cb: (complete: (value: T) => void, fail: (error: Error) => void) => void)

  
  

  松散参数类型


  class Promise<T>(cb: (complete, fail) => void) {
    then<V>(cb: (value: T), error: Error): Promise<V>
    catch(error: Error)
  }


  class Promise(cb) {
    then(cb, error)
    catch(cb)
  }
*/

function changePromise(): Promise<void> { 
  return new Promise<void>(function (complete, fail) {
    setTimeout(function () {
      timerButton.innerHTML = String(counter)
      counter++
      complete()
    }, 1000)
  })
}


var promise1 = changePromise()

var promise2 = promise1.then(function () {
  return changePromise()
})

var promise3 = promise2.then(function () {
  return changePromise()
})

var promise4 = promise3.then(function () {
  return changePromise()
})

var promise5 = promise4.then(function () {
  return changePromise()
})

var promise6 = promise5.then(function () {
  return changePromise()
})

var promise7 = promise6.then(function () {
  messageButton.innerHTML = 'Promise finished'
})


// changePromise()
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     messageButton.innerHTML = 'Promise2 finished'
//   })